import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'home',
    component: () => import( /* webpackChunkName: "about" */ '../views/home.vue'),

  },
  {
    path: '/login',
    name: 'login',
    component: () => import( /* webpackChunkName: "about" */ '../views/login.vue'),

  },
  {
    path: '/home',
    name: 'home',
    component: () => import( /* webpackChunkName: "about" */ '../views/home.vue'),
    children: [{
        path: '/users',
        name: 'users',
        component: () => import( /* webpackChunkName: "about" */ '../views/users.vue'),
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import( /* webpackChunkName: "about" */ '../views/roles.vue'),
      }, {
        path: '/rights',
        name: 'rights',
        component: () => import( /* webpackChunkName: "about" */ '../views/rights.vue'),
      }, {
        path: '/goods',
        name: 'goods',
        component: () => import( /* webpackChunkName: "about" */ '../views/goods.vue'),
      }, {
        path: '/categories',
        name: 'categories',
        component: () => import( /* webpackChunkName: "about" */ '../views/categories.vue'),
      }, {
        path: '/params',
        name: 'params',
        component: () => import( /* webpackChunkName: "about" */ '../views/params.vue'),
      }, {
        path: '/goods/add',
        name: 'add',
        component: () => import( /* webpackChunkName: "about" */ '../views/add.vue'),
      }, {
        path: '/reports',
        name: 'reports',
        component: () => import( /* webpackChunkName: "about" */ '../views/reports.vue'),
      }, {
        path: '/orders',
        name: 'orders',
        component: () => import( /* webpackChunkName: "about" */ '../views/orders.vue'),
      }
      , {
        path: '/index',
        name: 'index',
        component: () => import( /* webpackChunkName: "about" */ '../views/orders.vue'),
      }
    ]

  }
]

const router = new VueRouter({
  routes
})
//路由守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/home") {
    if (!localStorage.getItem("userinfo")) {
      next("/login")
    }
  } else if (to.path == "/login") {
    if (localStorage.getItem("userinfo")) {
      next("/home")
    }
  } 
  next()
})

export default router